/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.modules.api.controller.AppLogController.java 
 * <p>
 * 类产生时间: 2014-5-6 上午 10:04
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package com.oceansoft.mobile.econsole.modules.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.econsole.common.util.StringUtils;
import com.oceansoft.mobile.econsole.modules.accident.entity.AccidentInfo;
import com.oceansoft.mobile.econsole.modules.api.entity.CrashLog;
import com.oceansoft.mobile.econsole.modules.api.service.IAppLogService;
import com.oceansoft.mobile.econsole.modules.api.util.AppLogUtil;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.log.entity.AccessLog;
import com.oceansoft.mobile.econsole.modules.log.service.IAccessLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 客户端日志采集
 *
 * @author: chenw
 * @time: 2014-5-6 上午 10:04
 */
@RestController
@RequestMapping("api/log")
public class AppLogController extends BaseController {

    @Resource
    private IAppLogService appLogService;

    @Resource
    private IAccessLogService accessLogService;

    @RequestMapping("crash")
    public Result reportCrashLog(HttpServletRequest request) {
        String logJson = request.getParameter("resultData");
        Result response = new Result();
        CrashLog log = JSONObject.parseObject(logJson, CrashLog.class);
        try {
            log("记录客户端日志成功：" + logJson);
            return appLogService.save(log);
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSucc(Result.FAILURE);
            log("记录客户端日志失败：" + logJson);
        }
        return null;
    }

    @RequestMapping("feedback")
    public void feedback() {
    }

    @RequestMapping(value = "api", method = RequestMethod.POST)
    public Result addCrashLog(HttpServletRequest request) {
        String logJson = request.getParameter("resultData");
        Result response = new Result();
        JSONObject object = JSONObject.parseObject(logJson);
        CrashLog log = JSONObject.parseObject(logJson, CrashLog.class);
        try {
            AppLogUtil.report(log);
            log("记录客户端日志成功：" + logJson);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSucc(Result.FAILURE);
            log("记录客户端日志失败：" + logJson);
        }
        return response;
    }

    /**
     * 记录应用访问日志
     *
     * @return Result
     */
    @RequestMapping(value = "access", method = RequestMethod.POST)
    public Result addAppAccessLog(HttpServletRequest request) {
        try {
            AccessLog accessLog = buildAccess(request);
            accessLog.setAcsType(AccessLog.ASC_ACCESS);
            accessLogService.save(accessLog);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "访问记录保存失败");
        }
        return new Result("访问记录保存成功");
    }

    /**
     * 记录应用下载日志
     *
     * @return Result
     */
    @RequestMapping(value = "download", method = RequestMethod.POST)
    public Result addAppDownloadLog(HttpServletRequest request) {
        try {
            AccessLog accessLog = buildAccess(request);
            accessLog.setAcsType(AccessLog.ASC_DOWN);
            accessLogService.save(accessLog);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "下载记录保存失败");
        }
        return new Result("下载记录保存成功");
    }
    
    /**
     * 记录应用使用日志
     *
     * @return Result
     */
    @RequestMapping(value = "use", method = RequestMethod.POST)
    public Result addAppUseLog(HttpServletRequest request) {
        try {
            AccessLog accessLog = buildAccess(request);
            accessLog.setAcsType(AccessLog.ASC_USE);
            accessLogService.save(accessLog);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.FAILURE, "使用记录保存失败");
        }
        return new Result("使用记录保存成功");
    }

    private AccessLog buildAccess(HttpServletRequest request) {
        AccessLog accessLog = new AccessLog();
        String appId = request.getParameter("appId");
        String userId = request.getParameter("userId");
        String acsIp = request.getParameter("acsIp");
        String acsEqm = request.getParameter("acsEqm");
        String acsNet = request.getParameter("acsNet");
        String os_type = request.getParameter("osType");
        
        if (StringUtils.isNotEmpty(os_type))
        	accessLog.setOsType(Integer.parseInt(os_type));
        if (StringUtils.isNotEmpty(appId))
            accessLog.setAppId(Integer.parseInt(appId));
        if (StringUtils.isNotEmpty(userId))
            accessLog.setUserId(Integer.parseInt(userId));
        if (StringUtils.isNotEmpty(acsIp))
            accessLog.setAcsIp(acsIp);
        if (StringUtils.isNotEmpty(acsEqm))
            accessLog.setAcsEqm(acsEqm);
        if (StringUtils.isNotEmpty(acsNet))
            accessLog.setAcsNet(acsNet);

        accessLog.setAcsTime(new Date());
        return accessLog;
    }
}
